<template>
	<view>
		<uni-table :loading="isLoading" border stripe>
			<uni-tr>
				<uni-th align="center">阶段</uni-th>
				<uni-th align="center">目标</uni-th>
				<uni-th width="center" align="center">倍率</uni-th>
				<uni-th width="center" align="center">操作</uni-th>
			</uni-tr>
			<uni-tr v-for="(item,index) in data" :key="index">
				<uni-td align="center">{{item.name}}</uni-td>
				<uni-td align="center">{{item.mubiao}}</uni-td>
				<uni-td align="center">{{item.beilv}}</uni-td>
				<uni-td align="center">
					<view class="uni-group">
						<button @click="update(item)" class="uni-button" size="mini" type="primary">修改</button>
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>
		<uni-popup ref="passwordPopup" type="center">
			<view class="modal" style="width:450px; padding: 20px;">
				
				<view class="uni-container">
					<uni-forms ref="form" validateTrigger="bind">
						<uni-forms-item label="名称" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="名称" v-model="changedata.name" disabled=true/>
						</uni-forms-item>
						<uni-forms-item label="价格上浮次数" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="价格上浮次数" v-model="changedata.mubiao"/>
						</uni-forms-item>
						<uni-forms-item label="初始价格" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="初始价格" v-model="changedata.beilv"/>
						</uni-forms-item>
						<view class="uni-button-group pointer">
							<button class="uni-button uni-button-full" type="primary" :disabled="isLoading" @click="save">保存</button>
						</view>
					</uni-forms>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoading:false,
				data:[],
				changedata:{
					mubiao:"",
					name:"",
					beilv:""
				}
			}
		},
		methods: {
			getstage(){
				var self = this
				this.isLoading = true
				self.data = []
				this.$request('admin/loadconfig',{id:'stage'}).then(res => {
					this.isLoading = false
					var json = res.config
					console.log(json)
					for(let key in json){
						if(key != '_id'){
							var data = json[key]
							data['key'] = key
							self.data.push(data)
						}
					}
				}).catch(res => {
				
				}).finally(err => {
					this.isLoading = false
				})
			},
			update(data){
				var self = this
				self.changedata = data
				this.$refs.passwordPopup.open()
			},
			save(){
				var self = this
				self.isLoading = true
				var json = {}
				var data = {}
                console.log(self.data)
                for(var i=0;i<self.data.length;i++){
                    var item = self.data[i]
                    var key = item['key']
                    if(key != undefined && key != 'undefined'){
                        delete item['key']
                        json[key] = item
                    }
                }
                console.log(json)
				for(let key in self.changedata){
					if(key != 'key'){
						data[key] = self.changedata[key]
					}
				}
                if(self.changedata['key'] != undefined && self.changedata['key'] != 'undefined'){
                    json[self.changedata['key']] = data
                }
				self.$request('admin/changestage',{json:JSON.stringify(json)}).then(res => {
					self.isLoading = false
					uni.showToast({
						title: "修改成功",
						icon: 'none',
						duration: 2000
					})
					this.$refs.passwordPopup.close()
					self.getstage()
				}).catch(res => {
				
				}).finally(err => {
					this.isLoading = false
				})
			},
		},
		mounted() {
			var self = this
			self.getstage()
		}
	}
</script>

<style>
	.modal {
		width: 100%;
		max-width: 980px;
		margin: 0 auto;
		background-color: #FFFFFF;
	}
	
	.modal-content {
		padding: 15px;
		height: 500px;
		box-sizing: border-box;
	}
	
	.password-popup {
		padding: 30px;
	}
</style>
